﻿;#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
;use Input mode cause Alt+Up incorrect on windows resource manager
;SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
SetStoreCapslockMode off
Process ,Priority, ,High

currentState := 0
keyCount := 0
keyPressed := GetKeyState("CapsLock", "T")
pressTime = 
funcKeyPressed := false
ShowMainToolTip()
SetTimer TooltipTimer, 500
mapESC := true
pNum = %0%
if(pNum > 0)
{	
	mapESC = %1%
}

return

TooltipTimer:
ShowMainToolTip()
ShowCapsToolTip(GetKeyState("CapsLock", "T"))
return

DisableWinLock(d)
{
	RegWrite, REG_DWORD, HKEY_CURRENT_USER, Software\Microsoft\Windows\CurrentVersion\Policies\System, DisableLockWorkstation, %d%
}
SetState(v)
{
	global currentState
	currentState := v
}
ShowMainToolTip()
{
global currentState
Tooltip, %currentState%, 0, 0, 4
ttext = SuperKeyboard
if(currentState == 0)
	ttext = 
else if(currentState == 2)
	ttext = NumPad

Tooltip, %ttext%, 200, 0, 3
}

ShowCapsToolTip(show)
{
	ttext = 
	if(show)
		ttext = CAPS
	Tooltip ,%ttext%, 150, 0, 1
}
functionKeyPressed()
{
global funcKeyPressed
funcKeyPressed := true
}

$*CapsLock::

if(keyPressed)
	return

keyPressed := true
pressTime := A_TickCount
tmpKeyPressed := false
keyCount := keyCount + 1
SetTimer CapsLockTimer, 400
currentState := currentState + 1
currentState := mod(currentState, 3)

DisableWinLock(currentState == 1)

ShowMainToolTip()

if(keyCount == 2)
{
	SetTimer CapsLockTimer, off
	keyCount := 0
	state := GetKeyState("Capslock", "T")
	if(state)
		SetCapsLockState Off
	else
		SetCapsLockState On

	ShowCapsToolTip(!state)
}
return

$*CapsLock up::
global pressTime

if(!keyPressed)
	return

keyPressed := false
dur := A_TickCount - pressTime
if(tmpKeyPressed || dur > 800)
{
	keyCount := 0
	currentState := 0
	ShowMainToolTip()
}

DisableWinLock(currentState == 1)

return

CapsLockTimer:
SetTimer CapsLockTimer, off
keyCount = 0
return

getModKey()
{
global tmpKeyPressed
tmpKeyPressed := true
key = 
if(GetKeyState("Ctrl"))
	key = %key%^
if(GetKeyState("Alt"))
	key = %key%!
if(GetKeyState("Shift"))
	key = %key%+
if(GetKeyState("LWin"))
	key = %key%#
if(GetKeyState("RWin"))
	key = %key%#
return key
}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
#if !mapESC
$+Esc::
Send ~
return

#if mapESC
$*`::
if(GetKeyState("Shift") && !GetKeyState("Alt") && !GetKeyState("Ctrl") && !GetKeyState("LWin") && !GetKeyState("RWin"))
{
	Send ~
}
else
{
	key := getModKey()
	Send %key%{Esc}
}
return

if mapESC
$Esc::
Send ``
return
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
#if currentState == 1
$*i::
functionKeyPressed()
key := getModKey()
Send %key%{Up}
return

#if currentState == 1
$*l::
functionKeyPressed()
key := getModKey()
Send %key%{Right}
return

#if currentState == 1
$*k::
functionKeyPressed()
key := getModKey()
Send %key%{Down}
return

#if currentState == 1
$*j::
functionKeyPressed()
key := getModKey()
Send %key%{Left}
return

#if currentState == 1
$*u::
functionKeyPressed()
key := getModKey()
Send %key%{Home}
return

#if currentState == 1
$*o::
functionKeyPressed()
key := getModKey()
Send %key%{End}
return

#if currentState == 1
$*p::
functionKeyPressed()
key := getModKey()
Send %key%{PgUp}
return

#if currentState == 1
$*;::
functionKeyPressed()
key := getModKey()
Send %key%{PgDn}
return

#if currentState == 1
$*d::
functionKeyPressed()
key := getModKey()
Send %key%{Del}
return

#if currentState == 1
$*h::
functionKeyPressed()
key := getModKey()
Send %key%{BackSpace}
return

#if currentState == 1
$*1::
functionKeyPressed()
key := getModKey()
Send %key%{F1}
return

#if currentState == 1
$*2::
functionKeyPressed()
key := getModKey()
Send %key%{F2}
return

#if currentState == 1
$*3::
functionKeyPressed()
key := getModKey()
Send %key%{F3}
return

#if currentState == 1
$*4::
functionKeyPressed()
key := getModKey()
Send %key%{F4}
return

#if currentState == 1
$*5::
functionKeyPressed()
key := getModKey()
Send %key%{F5}
return

#if currentState == 1
$*6::
functionKeyPressed()
key := getModKey()
Send %key%{F6}
return

#if currentState == 1
$*7::
functionKeyPressed()
key := getModKey()
Send %key%{F7}
return

#if currentState == 1
$*8::
functionKeyPressed()
key := getModKey()
Send %key%{F8}
return

#if currentState == 1
$*9::
functionKeyPressed()
key := getModKey()
Send %key%{F9}
return

#if currentState == 1
$*0::
functionKeyPressed()
key := getModKey()
Send %key%{F10}
return

#if currentState == 1
$*-::
functionKeyPressed()
key := getModKey()
Send %key%{F11}
return

#if currentState == 1
$*=::
functionKeyPressed()
key := getModKey()
Send %key%{F12}
return

#if currentState == 1
$*m::
functionKeyPressed()
key := getModKey()
Send %key%{AppsKey}
return

#if currentState == 2
$*m::
functionKeyPressed()
Send {Numpad1}
return

$*y::
global currentState
if(currentState == 1)
{
	currentState := 2
	ToolTip, 2, 1000, 0, 11
}
else if(currentState == 0)
{
	Send y
}
else if(currentState == 2)
{
	SetState(1)
}
return


leftButtonDown := false
#if currentState == 1
$*w::
if (leftButtonDown)
	return
leftButtonDown := true
functionKeyPressed()
key := getModKey()
Send %key%{LButton Down}
return

#if currentState == 1
$*w up::
leftButtonDown := false
Send {LButton up}
return

rightButtonDown := false
#if currentState == 1
$*r::
if (rightButtonDown)
	return
rightButtonDown := true
functionKeyPressed()
key := getModKey()
Send %key%{RButton Down}
return

#if currentState == 1
$*r up::
rightButtonDown := false
Send {RButton up}
return

middleButtonDown := false
#if currentState == 1
$*e::
if (middleButtonDown)
	return
middleButtonDown := true
functionKeyPressed()
key := getModKey()
Send %key%{MButton Down}
return

#if currentState == 1
$*e up::
middleButtonDown := false
Send {MButton up}
return